package com.happy3w.ssp.schoolmatehome.component.image.type.image;

import com.happy3w.ssp.schoolmatehome.component.image.DrawAction;
import com.happy3w.ssp.schoolmatehome.component.image.LinePrintContext;
import com.happy3w.ssp.schoolmatehome.component.image.TypePrinter;
import org.springframework.stereotype.Component;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.List;

@Component
public class ImagePrinter implements TypePrinter<ResImage> {
    @Override
    public List<DrawAction> buildActions(ResImage item, LinePrintContext context) {
        BufferedImage image = item.getImage();
        if (image.getWidth() > context.leftWidth()) {
            context.nextLine();
        }

        int newY = context.getLocation().y - (image.getHeight() + context.getRowHeight())/2;
        Point newLocation = new Point(context.getLocation().x, newY);
        context.getLocation().x += image.getWidth();

        return List.of(
                new DrawImageAction(image, newLocation)
        );
    }

    @Override
    public boolean isSupport(Object item) {
        return item instanceof ResImage;
    }
}
